<template>
  <div style="margin: 15px; margin-right: 30px;">
    <el-form ref="form" :rules="rules" :model="form" label-width="60px">
      <el-row>
        <el-col :span="24">
          <el-form-item label="姓名" prop="userName">
            <el-input v-model="form.userName" placeholder="请输入姓名" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="电话" prop="phone">
            <el-input v-model="form.phone" placeholder="请输入电话" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="角色" prop="staffType">
            <el-select v-model="form.staffType" placeholder="请选择角色" clearable>
              <el-option
                v-for="item in staffTypeOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="funcCancel">取 消</el-button>
      <el-button type="primary" @click="funcSubmit">确 定</el-button>
    </div>
  </div>
</template>
<script>
import Vue from 'vue'
import 'xe-utils'
import VXETable from 'vxe-table'
import 'vxe-table/lib/index.css'
Vue.use(VXETable)
import { addShipmentStaff } from '@/api/shipment'
import { validatePhone } from '@/utils/validate.js'
export default {
  name: 'ShipmentStaffAdd',
  props: {
    staffTypeOptions: {
      type: Array,
      default() {
        return []
      }
    },
    companyCode: {
      type: String,
      default() {
        return ''
      }
    }
  },
  data() {
    return {
      form: {
        userName: '',
        phone: '',
        staffType: ''
      },
      rules: {
        userName: [
          { required: true, message: '请输入姓名', trigger: 'blur' }
        ],
        phone: [
          {
            required: true,
            validator(rule, value, callback) {
              validatePhone(value)
                ? callback()
                : callback(new Error('手机号格式有误'))
            },
            trigger: 'blur'
          }
        ],
        staffType: [
          { required: true, message: '请选择角色', trigger: 'change' }
        ]
      }
    }
  },
  created() {},
  methods: {
    funcSubmit() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          const rLoading = this.openLoading();
          var data = Object.assign({}, this.form, { companyCode: this.companyCode })
          addShipmentStaff(data)
            .then(response => {
              rLoading.close();
              this.$message({ message: response.msg, type: 'success' })
              this.$emit('fatherAddMethod')
            })
            .then(() => this.$refs['form'].resetFields())
        } else {
          return false
        }
      })
    },
    // 取消
    funcCancel() {
      this.$emit('fatherCancelMethod', 'add')
    }
  }
}
</script>
<style lang="scss" scoped>
.dialog-footer {
  margin: 15px 0 15px 0;
  width: 175px;
  float: right;
}
.el-cascader {
  width: 100%;
}
.el-select {
  width: 100%;
}
</style>
